/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Operations;

import IFSv2.Models.mdlLogin;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Fulbert
 */
public class clsConnectionProvider {
    
    private String dbUsername;
    private String dbPassword;
    private String dbServer;
    private String dbPort;
    private String sqlServer;
    private Connection dbConn = null;

    public enum SQLServer {
        MSSQL, MySQL, Oracle
    }
    
    public clsConnectionProvider() {
        this.dbUsername = mdlLogin.dbUsername;
        this.dbPassword = mdlLogin.dbPassword;
        this.dbServer = mdlLogin.dbServer;
        this.dbPort = mdlLogin.dbPort;
        this.sqlServer = mdlLogin.sqlServer;
    }
    
    public boolean dbConnect() {
        
        switch(SQLServer.valueOf(sqlServer)) {
        
            case MSSQL:
                try {
                    MSSQLConnect();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, ex.toString().substring(ex.toString().indexOf(':')+1, ex.toString().length()), ex.toString().substring(0,ex.toString().lastIndexOf(':')), JOptionPane.ERROR_MESSAGE);
//                    System.exit(0);
                    return false;
                }
                
                break;
                
            case MySQL:
                try {
                    MySQLConnect();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, ex.toString().substring(ex.toString().indexOf(':')+1, ex.toString().length()), ex.toString().substring(0,ex.toString().lastIndexOf(':')), JOptionPane.ERROR_MESSAGE);
//                    System.exit(0);
                    return false;
                }
                break;
                
            case Oracle:
                break;
                
            default:
                break;
        }
        
        return true;
    }

    private void MSSQLConnect() throws SQLException {
        dbConn = DriverManager.getConnection("jdbc:sqlserver://"+dbServer, dbUsername, dbPassword);
    }
    
    private void MySQLConnect() throws SQLException {
        dbConn = DriverManager.getConnection("jdbc:mysql://"+dbServer+":"+dbPort, dbUsername, dbPassword);
    }

    public List<String> getDatabaseList() {
        
        ResultSet databaseList = null;
        DatabaseMetaData meta;
        List<String> dbList = new ArrayList();
        
        try{
            switch(SQLServer.valueOf(sqlServer)) {

                case MSSQL:
                case MySQL:
                        meta = dbConn.getMetaData();
                        databaseList = meta.getCatalogs();
                    break;
                case Oracle:    
                        meta = dbConn.getMetaData();
                        databaseList = meta.getSchemas();
                    break;
                default: 
                    break;
            }    
        
            while(databaseList.next()){
                dbList.add(databaseList.getString(1));
            }
            
            dbConn.close();
        }catch(SQLException ex) {}
        
        return dbList;
    }
}
